import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:opene/app_constant.dart';
import 'package:opene/app_theme.dart';
import 'package:opene/app_utils.dart';
import 'package:opene/base/base_state.dart';
import 'package:opene/base/loading.dart';
import 'package:opene/base/scan_code.dart';
import 'package:opene/net/method/base_method.dart';
import 'package:opene/net/network_manager.dart';
import 'package:opene/net/response/base_response.dart';
import 'package:opene/ui/fbatransfer/fbatransferscreen.dart';
import 'package:opene/ui/putaway/putawaydialogscreen.dart';
import 'package:toast/toast.dart';

class FBATransferPutawayScreen extends StatelessWidget {
  FBATransfer fbaTransfer;

  FBATransferPutawayScreen({this.fbaTransfer});

  @override
  Widget build(BuildContext context) {
    return FBATransferPutawayScreenPage(fbaTransfer: fbaTransfer);
  }
}

class FBATransferPutawayScreenPage extends StatefulWidget {
  FBATransfer fbaTransfer;

  FBATransferPutawayScreenPage({this.fbaTransfer});

  @override
  _FBATransferPutawayPageState createState() => _FBATransferPutawayPageState(fbaTransfer: fbaTransfer);
}

class _FBATransferPutawayPageState extends BaseState<FBATransferPutawayScreen> {
  FBATransfer fbaTransfer;

  _FBATransferPutawayPageState({this.fbaTransfer});


  @override
  String setTitle({String title}) {
    return !Utils.isEmpty(title) ? title : "FBATransfer: Submit";
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    next = "Submit";
  }

  @override
  Widget getViewBinding() {
    return Padding(
      padding: const EdgeInsets.only(left: 0, top: 10, right: 0),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            margin: EdgeInsets.only(left: 15),
            child: Row(
              children: [
                Text(
                  "IBLPN: ",
                  style: TextStyle(color: MyAppTheme.color_666666, fontSize: 14.0),
                ),
                Text(
                  Utils.getStringisEmpty(fbaTransfer.ibLpn),
                  style: TextStyle(color: MyAppTheme.color_333333, fontSize: 14.0),
                ),
              ],
            ),
          ),
          Container(
            margin: EdgeInsets.only(left: 15, right: 15),
            child: Row(
              children: [
                Image.asset(
                  'assets/images/ic_recommended.png',
                  width: 15,
                  height: 15,
                ),
                Expanded(
                  flex: 1,
                  child: Text(
                    " Location: ${Utils.getStringisEmpty(fbaTransfer.originalLocation)}",
                    style: TextStyle(color: MyAppTheme.color_333333, fontSize: 14.0),
                  ),
                ),
              ],
            ),
          ),
          Container(
            margin: const EdgeInsets.only(left: 15, top: 5, right: 15, bottom: 0),
            width: double.infinity,
            child: ConstrainedBox(
              constraints: BoxConstraints(maxHeight: 35),
              child: TextField(
                controller: textEditingController,
                textInputAction: TextInputAction.search,
                onSubmitted: (String text) {
                  print('Searching for $text');
                  if (Utils.isEmpty(text)) {
                    return;
                  }
                },
                // autofocus: true,
                decoration: InputDecoration(
                  suffixIcon: IconButton(
                    padding: EdgeInsets.all(2.0),
                    icon: Icon(
                      Icons.qr_code_scanner,
                      size: 28,
                      color: MyAppTheme.nearlyBlue,
                    ),
                    onPressed: () {
                      // requestPermissions();
                      // Utils.scanBarcode(context);
                      Navigator.of(context).push(MaterialPageRoute(builder: (context) {
                        return ScanCode();
                      })).then((text) {
                        if (text == null) {
                          return;
                        }
                        setState(() {
                          textEditingController.text = text;
                        });
                        print(text); //拿到扫描结果
                      });
                    },
                  ),
                  border: OutlineInputBorder(
                    borderSide: BorderSide(
                      color: MyAppTheme.nearlyBlue,
                    ),
                    borderRadius: const BorderRadius.all(Radius.circular(20.0)),
                  ),
                  enabledBorder: OutlineInputBorder(
                    borderSide: BorderSide(color: MyAppTheme.nearlyBlue),
                    // 设置焦点之外的边框颜色
                    borderRadius: const BorderRadius.all(Radius.circular(20.0)),
                  ),
                  focusedBorder: OutlineInputBorder(
                    borderSide: BorderSide(color: MyAppTheme.nearlyBlue),
                    // 设置焦点时的边框颜色
                    borderRadius: const BorderRadius.all(Radius.circular(20.0)),
                  ),
                  hintText: "scan racking location barcode",
                  filled: true,
                  fillColor: MyAppTheme.white,
                  //隐藏下划线
                  //border: InputBorder.none,
                  hintStyle: TextStyle(color: Colors.grey),
                  contentPadding: const EdgeInsets.symmetric(vertical: 0, horizontal: 15),
                ),
                onChanged: (text) {
                  code = text;
                },
              ),
            ),
          ),
        ],
      ),
    );
  }

  void racking() async {
    fbaTransfer.transLocation = textEditingController.text;
    // 显示加载中对话框
    LoadingIndicator.show(context, title: "Submit...");
    String json = jsonEncode(fbaTransfer);

    BaseResponse baseResponse = await NetworkManager().request(context, Constants.cmsTransferSubmit, method: BaseMethod.post, data: json);
    // 在需要的时候隐藏加载中对话框
    LoadingIndicator.hide();

    if (baseResponse.status == 200) {
      showDialog(
        context: context,
        barrierDismissible: false, // 设置背景不可点击
        builder: (context) => PutawaySucceedDialog(lpn: fbaTransfer.ibLpn,),
      ).then((value) => {Navigator.pop(context, true)});
    } else {
      ToastContext().init(context);
      Toast.show(Utils.getStringisEmpty(baseResponse.msg), duration: Toast.lengthShort, gravity: Toast.bottom);
    }
  }

  @override
  void showFileUploadData(String path, int type, int position, bool isSubmit) {
    // TODO: implement showFileUploadData
  }

  @override
  void setNext() {
    if (Utils.isEmpty(textEditingController.text)) {
      ToastContext().init(context);
      Toast.show("Please scan the shelf position", duration: Toast.lengthShort, gravity: Toast.bottom);
      return;
    }
    if ((!Utils.isEmpty(fbaTransfer.originalLocation)) && (textEditingController.text != fbaTransfer.originalLocation)) {
      Toast.show("The new location is the same as the original location", duration: Toast.lengthShort, gravity: Toast.bottom);
      return;
    }

    racking();
  }

  @override
  void setReset() {
    setState(() {
      textEditingController.text = "";
      hint = "";
    });
  }
}
